package onion.molingo2o.activity.ablumimage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.easemob.chatuidemo.ui.BaseActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import onion.molingo2o.Config;
import onion.molingo2o.R;
import onion.molingo2o.utils.ImageModel;
import onion.molingo2o.utils.imageutil.BitmapHelper;


/**
 * Created by Administrator on 2015/9/29.
 */
public class ShowAllImgActivtiy extends BaseActivity {
    private ViewPager vp_pic;

    private ArrayList<ImageModel> imageModelList = new ArrayList<>();
    private MyPagerAdapter adapter;
    private int totalcount;
    private ImageView image;
    private List<ImageView> list;
    private Button btn_image_selected;
    private ImageView iv_back, iv_share;
//    MulitPointTouchListener mptl;

    private int lastPosition;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showallimage_vp);
        imageModelList.addAll((List) getIntent().getSerializableExtra(Config.EXTRA_IMAGE_LIST));
        lastPosition = getIntent().getIntExtra("position", 0);
        totalcount = getIntent().getIntExtra(Config.INTENT_PARMAS1, 0);
        initView();
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_share = (ImageView) findViewById(R.id.iv_share);
        vp_pic = (ViewPager) findViewById(R.id.vp_pic);
        list = new ArrayList<ImageView>();
        for (int i = 0; i < imageModelList.size(); i++) {
            list.add(new ImageView(this));
        }
        //创建适配器， 把组装完的组件传递进去
        adapter = new MyPagerAdapter(list);
        vp_pic.setAdapter(adapter);
//        vp_pic.setOnClickListener(this);
        btn_image_selected = (Button) findViewById(R.id.btn_image_selected);
        btn_image_selected.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

//                if (BitmapHelper.act_bool) {
//                    logger.info("go to newActivity ");
//                    logger.info("act_boll is :"+BitmapHelper.act_bool);
//                    Intent intent = new Intent(AlbumDetailActivity.this,NewActivityActivity.class);
//                    startActivity(intent);
//                    BitmapHelper.act_bool = false;
//                }
                Intent intentPop = new Intent();
//                BitmapHelper.addAll(paths, totalcount);
                setResult(RESULT_OK, intentPop);

                finish();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
                finish();
            }
        });
        iv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = imageModelList.get(lastPosition).imagePath;
                ImageModel item = imageModelList.get(lastPosition);
                if (totalcount < Config.SelectImgCount) {
                    item.isSelected = !item.isSelected;
                    if (item.isSelected) {
                        iv_share.setSelected(true);
                        totalcount++;
                        changeComplete(totalcount);
                        AlbumDetailActivity.pathList.add(path);
                    } else {
                        iv_share.setSelected(false);
                        totalcount--;
                        changeComplete(totalcount);
                        AlbumDetailActivity.pathList.remove(path);
                        BitmapHelper.removeBitmap(path);
                    }
                } else if (item.isSelected) {
                    item.isSelected = false;
                    iv_share.setSelected(false);
                    totalcount--;
                    changeComplete(totalcount);
                    AlbumDetailActivity.pathList.remove(path);
                    BitmapHelper.removeBitmap(path);
                } else {
                    showToast("最多选择" + Config.SelectImgCount + "张图片");
//                    }
                }
            }
        });
        vp_pic.setOnPageChangeListener(new MyListener());
        vp_pic.setCurrentItem(lastPosition);
        iv_share.setSelected(imageModelList.get(lastPosition).isSelected);
        changeComplete(totalcount);
    }

    private void changeComplete(int count) {
        if (count > 0) {
            btn_image_selected.setSelected(true);
            btn_image_selected.setClickable(true);
            btn_image_selected.setText("完成" + "(" + count + ")");
        } else {
            btn_image_selected.setSelected(false);
            btn_image_selected.setClickable(false);
            btn_image_selected.setText("完成");
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void back() {
        Intent data = new Intent();
        data.putExtra(Config.EXTRA_IMAGE_LIST, imageModelList);
        data.putExtra(Config.INTENT_PARMAS1, totalcount);
        setResult(RESULT_CANCELED, data);
    }

//    public void onClick(View v) {
//        imageModelList = null;
//        adapter = null;
//        list = null;
//        finish();
//    }

    /**
     * 动作监听器，可异步加载图片
     */
    private class MyListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int state) {
            // TODO Auto-generated method stub
        }


        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int position) {
            //initPointGroup(position);
           iv_share.setSelected( imageModelList.get(position).isSelected);
            lastPosition = position;
        }


    }

    private class MyPagerAdapter extends PagerAdapter {
        private List<ImageView> mList;

        public MyPagerAdapter(List<ImageView> list) {
            mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
            // 释放资源
            // object = null;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(final ViewGroup container, final int position) {
            // mList.get(position).setOnClickListener(ShowBigImageViewPager.this);
            container.addView(mList.get(position));
            ImageLoader.getInstance().displayImage("file://" + imageModelList.get(position).imagePath, mList.get(position));
//            mptl = new MulitPointTouchListener();
//            mList.get(position).setOnTouchListener(mptl);
            return mList.get(position);
        }
    }
//    public static DisplayImageOptions options = new DisplayImageOptions.Builder()
//            .showImageOnLoading(R.drawable.ic_launcher)
//            .showImageForEmptyUri(R.drawable.ic_launcher)
//            .showImageOnFail(R.drawable.ic_launcher)
//            .cacheInMemory(true)
//            .cacheOnDisk(true)
//            .considerExifParams(true)
////            .displayer(new RoundedBitmapDisplayer(20))
//            .build();

}
